package edu.cug.water.crawler.job.task.committee;


import edu.cug.water.crawler.common.constant.Constant;
import edu.cug.water.crawler.common.constant.EsConstants;
import edu.cug.water.crawler.common.exception.RRException;
import edu.cug.water.crawler.common.utils.DateUtils;
import edu.cug.water.crawler.common.utils.EsUtils;
import edu.cug.water.crawler.job.entity.committee.YellowRiver;
import edu.cug.water.crawler.job.request.RequestUtil;
import edu.cug.water.crawler.job.task.ITask;
import org.frameworkset.util.LinkedMultiValueMap;
import org.frameworkset.util.MultiValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component("job:YellowRiver")
public class YellowRiverTask implements ITask {

    private static final String REGEX = "<td>([^nbsp]*?)</td>";
    private static final String REGEX_PARAM = "<input.*name=\"([^\".]*)\".*value=\"([^\".]*)\"";
    private static Pattern pattern = Pattern.compile(REGEX);
    private static Pattern pattern_param = Pattern.compile(REGEX_PARAM);


    private Logger logger = LoggerFactory.getLogger(getClass());

    private static final String url = "http://61.163.88.227:8006/hwsq.aspx?sr=0nkRxv6s9CTRMlwRgmfFF6jTpJPtAv87";

    @Autowired
    private EsUtils esUtils;

    @Autowired
    private RequestUtil requestUtil;

    @Override
    public int run(String date) throws IOException {

        String index = requestIndex();
        List<YellowRiver> data = null;

        if (date == null || "".equals(date) || DateUtils.formatDate(new Date()).equals(date.trim())) {
            date = DateUtils.formatDate(new Date());
            data = parseDate(index, date);
        } else {
            Map<String, String> paramFromIndex = getParamFromIndex(index);
            String postData = requestPOST(paramFromIndex, date.trim());
            data = parseDate(postData, date);
        }
        Boolean aBoolean = esUtils.statusJudge(esUtils.addDocuments(EsConstants.TypeConstant.YELLOW_RIVER.getValue(), data));
        if (!aBoolean)
            throw new RRException(getClass().getName() + Constant.LogConstants.THE_PROCESS_OF_ES_STORE_FAILED.getValue());

        return data.size();
    }

    private Map<String, String> getParamFromIndex(String index) {
        Matcher matcher = pattern_param.matcher(index);
        Map<String, String> map = new HashMap<>();

        map.put("ctl00$ScriptManager1", "ctl00$ScriptManager1|ctl00$ContentLeft$Button1");
        map.put("__EVENTTARGET", "");
        map.put("__EVENTARGUMENT", "");
        map.put("ctl00$ContentLeft$menuDate1$TextBox11", "2021-10-26");
        map.put("__VIEWSTATE", "/wEPDwULLTEwMDI5NzA1NzkPZBYCZg9kFgICAw9kFgICBQ9kFgJmD2QWAgIBD2QWAgIBDxYCHglpbm5lcmh0bWwF9Ew8dGFibGUgd2lkdGg9Ijk4JSIgYm9yZGVyPSIwIiBjZWxscGFkZGluZz0iMCIgY2VsbHNwYWNpbmc9IjEiIGJnY29sb3I9IiNEMUREQUEiIGFsaWduPSJjZW50ZXIiPjx0cj48dGQgaGVpZ2h0PSI0MCIgYmFja2dyb3VuZD0ic2tpbi9pbWFnZXMvbmV3bGluZWJnMy5naWYiPjx0YWJsZSB3aWR0aD0iOTglIiBib3JkZXI9IjAiIGNlbGxzcGFjaW5nPSIwIiBjZWxscGFkZGluZz0iMCI+PHRyPjx0ZCBhbGlnbj0iY2VudGVyIj48ZGl2IGNsYXNzPSdmaXJzdFRpdGxlJz7msLTmg4Xml6XmiqU8L2Rpdj48ZGl2IGNsYXNzPSdzZWNUaXRsZSc+MjAyMS0xMC0yNjwvZGl2PjwvdGQ+PC90cj48L3RhYmxlPjwvdGQ+PC90cj48L3RhYmxlPjx0YWJsZSB3aWR0aD0iOTglIiBib3JkZXI9IjAiIGNlbGxwYWRkaW5nPSIyIiBjZWxsc3BhY2luZz0iMSIgYmdjb2xvcj0iI0QxRERBQSIgYWxpZ249ImNlbnRlciIgc3R5bGU9Im1hcmdpbi10b3A6OHB4IiBjbGFzcz0ibWFpblR4dCI+PHRyPjx0ZCB3aWR0aD0iNTAlIj48dGFibGUgd2lkdGg9IjEwMCUiIGJvcmRlcj0iMCIgY2VsbHBhZGRpbmc9IjIiIGNlbGxzcGF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gwPC90ZD48dGQ+NC41MTwvdGQ+PC90cj48dHIgYWxpZ249ImNlbnRlciIgYmdjb2xvcj0iI0ZGRkZGRiI+PHRkPum7hOaysyAgICAgICAgICAgICAgICAgICAgICAgICAgPC90ZD48dGQ+5Yip5rSlICAgICAgICAgICAgICAgICAgICAgICAgICA8L3RkPjx0ZD4xMC4wMzwvdGQ+PHRkPjIyNzA8L3RkPjx0ZD41LjM1PC90ZD48L3RyPjwvdGFibGU+PC90ZD48L3RyPjwvdGFibGU+ZGSzbU9Bi5TozRq6QZeLm7ptMAPiimtmeUSHhhOz1Q0zDQ==");
        map.put("__VIEWSTATEGENERATOR", "E4DC7756");
        map.put("__EVENTVALIDATION", "/wEdAAO1jrUBRSSWrDwlaocokZD29DkLBAR+UXBBGQ1m5cY+HY5Ggl8DGIT46Qo2GBY6Yh4VqNut0DYODvghCRnvmL8LaeRq+rXfe9W/zOqtCPHwwQ==");
        map.put("__ASYNCPOST", "true");
        map.put("ctl00$ContentLeft$Button1", "查询");

        /*while(matcher.find()){
            if(matcher.groupCount() != 2) {
                logger.error("YellowRiverTask GetParamFromIndex 正则失败！");
                throw new RRException("YellowRiverTask GetParamFromIndex 正则失败！");
            }
            map.put(matcher.group(1), matcher.group(2));
        }*/
        return map;
    }

    private List<YellowRiver> parseDate(String index, String date) {
        Matcher matcher = pattern.matcher(index);

        List<YellowRiver> results = new ArrayList<>();

        List<String> list = new ArrayList<>();
        while(matcher.find()) {
            list.add(matcher.group(1).trim());
        }

        for (int i = 0, base = 5; i < list.size() / base; i ++) {
            YellowRiver yellowRiver = new YellowRiver();
            yellowRiver.setRiverName(list.get(i * base).trim()).
                    setStationName(list.get(i * base + 1).trim()).
                    setWaterStage(list.get(i * base + 2).trim()).
                    setFlow(list.get(i * base + 3).trim()).
                    setGrittiness(list.get(i * base + 4).trim()).
                    setCreateTime(DateUtils.formatDateTime(new Date())).
                    setDate(date);

            results.add(yellowRiver);
        }
        logger.info("YellowRiverTask Data Convert Results: {}", results.size());
        if (results.size() == 0) throw new RRException("YellowRiverTask Data Convert Results: 0");

        return results;
    }

    private String requestIndex() {
        HttpHeaders header = new HttpHeaders();
        header.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        header.set("Host", "www.cjh.com.cn");
        header.set("Connection", "keep-alive");
        header.set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        header.set("X-Requested-With", "XMLHttpRequest");
        header.set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36");
        header.set("Origin", "http://106.37.208.243:8068");
        header.set("Referer", "http://www.cjh.com.cn/swyb_sssq.html");
        header.set("Accept-Encoding", "gzip, deflate");
        header.set("Accept-Language", "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7");

        HttpEntity<Object> entity = new HttpEntity<>(null, header);
        ResponseEntity<String> response = requestUtil.request(url, HttpMethod.GET, entity);

        logger.info("YellowRiver Task status code: {}", response.getStatusCode());

        if ("".equals(response.getBody()) || response.getBody() == null) {
            logger.error("YellowRiver response.getBody() is null or \"\" ");
            throw new RRException("Yellow River Task get repsonse body for request index page has failed: null");
        } else {
            return response.getBody();
        }
    }

    private String requestPOST(Map<String, String> paramFromIndex, String date) {
        HttpHeaders header = new HttpHeaders();

        header.set("Host", "61.163.88.227:8006");
        header.set("Connection", "keep-alive");
        header.set("Cache-Control", "no-cache");
        //header.set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        header.set("Accept", "*/*");
        header.set("X-Requested-With", "XMLHttpRequest");
        header.set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36");
        header.set("Origin", "http://61.163.88.227:8006");
        header.set("Referer", "http://61.163.88.227:8006/hwsq.aspx?sr=0nkRxv6s9CTRMlwRgmfFF6jTpJPtAv87");
        header.set("Accept-Encoding", "gzip, deflate");
        header.set("Accept-Language", "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7");
        header.set("X-MicrosoftAjax", "Delta=true");


        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();

        map.setAll(paramFromIndex);
        map.set("ctl00$ScriptManager1", "ctl00$ScriptManager1|ctl00$ContentLeft$Button1");
        map.set("ctl00$ContentLeft$menuDate1$TextBox11", date);
        map.set("__ASYNCPOST","true");
        map.set("ctl00$ContentLeft$Button1", "查询");

        HttpEntity<MultiValueMap> entity = new HttpEntity<>(map, header);

        ResponseEntity<String> response = requestUtil.request(url, HttpMethod.POST, entity);

        logger.info("YellowRiver Task status code: {}", response.getStatusCode());

        if ("".equals(response.getBody()) || response.getBody() == null) {
            logger.error("YellowRiver response.getBody() is null or \"\" ");
            throw new RRException("Yellow River Task get repsonse body for request post data has failed: null");
        } else {
            return response.getBody();
        }
    }

}
